from xjtusom_list import XjtusomList
import sys
class XjtusomAList(XjtusomList):

    def __init__(self, size=10):
        self._msize = size
        self._listArray = [0] * size
        self._numInList = 0

    def clear(self):
        self._numInList = 0

    def insert(self, item) -> bool:
        if self._numInList == self._msize:
            return False
        if self.search(item):
            print("The item is already in the list")
            return False
        self._listArray[self._numInList] = item
        self._numInList += 1
        return True

    def search(self, item) -> bool:
        for i in range(self._numInList):
            if self._listArray[i] == item:
                return True
        return False

    def delete(self, item) -> bool:
        itemIndex = -1
        for i in range(self._numInList):
            if self._listArray[i] == item:
                itemIndex = i
                break
        if itemIndex == -1:
            return False
        for i in range(itemIndex, self._numInList-1):
            self._listArray[i] = self._listArray[i+1]
        self._numInList -= 1
        return True

    def successor(self, item):
        itemIndex = -1
        for i in range(self._numInList):
            if self._listArray[i] == item:
                itemIndex = i
                break
        if itemIndex == -1 or itemIndex + 1 == self._numInList:
            return -1
        else :
            return self._listArray[itemIndex+1]

    def predecessor(self, item):
        itemIndex = -1
        for i in range(self._numInList):
            if self._listArray[i] == item:
                itemIndex = i
                break
        if itemIndex == -1 or itemIndex == 0:
            return -1
        else :
            return self._listArray[itemIndex-1]

    def minimum(self):
        _min = sys.maxsize
        for i in range(self._numInList):
            if self._listArray[i] < _min:
                _min = self._listArray[i]
        if _min == sys.maxsize:
            return -1
        return _min
    def maximum(self):
        _max = -1
        for i in range(self._numInList):
            if self._listArray[i] > _max:
                _max = self._listArray[i]
        return _max
    def KthElement(self, k):
        if self._numInList == 0 or k < 1 or k > self._numInList:
            return -1
        if k == 1:
            return self.minimum()
        if k == self._numInList:
            return self.maximum()
        temp = self._listArray[0:self._numInList]
        temp.sort()
        return temp[k-1]

def main():
    myList = XjtusomAList()
    assert(myList.search(1) == False)
    myList.insert(1)
    myList.insert(10)
    assert(myList.insert(2) == True)
    assert(myList.insert(10) == False)
    assert(myList.search(2) == True)
    assert(myList.maximum() == 10)
    assert(myList.minimum() == 1)
    assert(myList.KthElement(4) == -1)
    assert(myList.KthElement(2) == 2)
    myList.insert(5)
    myList.insert(8)
    myList.insert(7)
    myList.insert(9)
    myList.insert(0)
    myList.insert(4)
    myList.insert(6)
    assert(myList.insert(3) == False)
    assert(myList.maximum() == 10)
    assert(myList.minimum() == 0)
    assert(myList.KthElement(4) == 4)
    assert(myList.delete(0) == True)
    assert(myList.delete(3) == False)
    assert(myList.successor(5) == 8)
    assert(myList.predecessor(5) == 2)
    assert(myList.successor(6) == -1)
    assert(myList.predecessor(1) == -1)

if __name__ == "__main__":
    main()
